//
//  FollowSetView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/10.
//

#import "FollowSetView.h"


@interface FollowSetView ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) UIButton *autoBtn;
@property (nonatomic, strong) UIButton *handBtn;

@property (nonatomic, strong) UIButton *cancelBtn;

@end

@implementation FollowSetView

- (void)initViews {
    self.backgroundColor = kRGB_ALPHA(0x000000, 0.5);
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.bottomView];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"设置";
    titleLabel.font = kFontRegula(14);
    titleLabel.textColor = kRGB(0x999999);
    [self.topView addSubview:titleLabel];
    
    [self.topView addSubview:self.autoBtn];
    [self.topView addSubview:self.handBtn];
    
    [self.bottomView addSubview:self.cancelBtn];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(20);
    }];
    [self.handBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(0);
        make.height.mas_equalTo(68);
    }];
    [self.autoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(0);
        make.height.mas_equalTo(68);
        make.bottom.mas_equalTo(self.handBtn.mas_top);
    }];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)btnAction:(UIButton *)sender {
    if ((self.autoBtn.selected && sender == self.autoBtn) || (self.handBtn.selected && sender == self.handBtn)) {
        return;
    }
    self.handBtn.selected = NO;
    self.autoBtn.selected = NO;
    sender.selected = YES;
    if (self.followSetBlock) {
        self.followSetBlock(1002-sender.tag);
    }
    if (sender == self.autoBtn) {
        self.autoBtn.backgroundColor = kRGB_ALPHA(0xFF9754, 0.1);
        self.handBtn.backgroundColor = [UIColor whiteColor];
        
        NSString *str = @"默认：跟随商家自动上架，自动下架\nps:只有关注商家后才会,该功能跟随商家账号绑定,如商家二维码被封,上游商家更换二维码即可,系统会自动上下架";
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
        NSRange range = [str rangeOfString:@"默认：跟随商家自动上架，自动下架"];
        [attr addAttributes:@{NSFontAttributeName:kFontMediium(14)} range:range];
        [attr addAttributes:@{NSForegroundColorAttributeName:kBgColor} range:NSMakeRange(0, str.length)];
        [self.autoBtn setAttributedTitle:attr forState:UIControlStateNormal];
    }else {
        self.handBtn.backgroundColor = kRGB_ALPHA(0xFF9754, 0.1);
        self.autoBtn.backgroundColor = [UIColor whiteColor];
        
        NSString *str = @"默认：跟随商家自动上架，自动下架\nps:只有关注商家后才会,该功能跟随商家账号绑定,如商家二维码被封,上游商家更换二维码即可,系统会自动上下架";
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
        NSRange range = [str rangeOfString:@"默认：跟随商家自动上架，自动下架"];
        [attr addAttributes:@{NSFontAttributeName:kFontMediium(14)} range:range];
        [attr addAttributes:@{NSForegroundColorAttributeName:kRGB(0x1B1B1B)} range:NSMakeRange(0, str.length)];
        [self.autoBtn setAttributedTitle:attr forState:UIControlStateNormal];
    }
    [self hide];
}

- (void)setUpStatus:(BOOL)upStatus {
    _upStatus = upStatus;
    [self btnAction:upStatus ? self.autoBtn : self.handBtn];
}

#pragma maek - 懒加载
- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(16, (kHeightForScreen-300), kWidthForScreen-32, 300)];
        _contentView.backgroundColor = [UIColor clearColor];
    }
    return _contentView;
}

- (UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthForScreen-32, 190)];
        _topView.backgroundColor = [UIColor whiteColor];
        _topView.layer.cornerRadius = 16;
        _topView.clipsToBounds = YES;
    }
    return _topView;
}

- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, kWidthForScreen-32, 60)];
        _bottomView.backgroundColor = [UIColor whiteColor];
        _bottomView.layer.cornerRadius = 16;
    }
    return _bottomView;
}

- (UIButton *)autoBtn {
    if (!_autoBtn) {
        _autoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_autoBtn setTitleColor:kRGB(0x1B1B1B) forState:UIControlStateNormal];
        [_autoBtn setTitleColor:kBgColor forState:UIControlStateSelected];
        _autoBtn.titleLabel.font = kFontRegula(12);
        _autoBtn.tag = 1001;
        NSString *str = @"默认：跟随商家自动上架，自动下架\nps:只有关注商家后才会,该功能跟随商家账号绑定,如商家二维码被封,上游商家更换二维码即可,系统会自动上下架";
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
        NSRange range = [str rangeOfString:@"默认：跟随商家自动上架，自动下架"];
        [attr addAttributes:@{NSFontAttributeName:kFontMediium(14)} range:range];
        [attr addAttributes:@{NSForegroundColorAttributeName:kBgColor} range:NSMakeRange(0, str.length)];
        [_autoBtn setAttributedTitle:attr forState:UIControlStateNormal];
        _autoBtn.backgroundColor = kRGB_ALPHA(0xFF9754, 0.1);
        _autoBtn.titleLabel.numberOfLines = 0;
        _autoBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        _autoBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 30);
        _autoBtn.selected = YES;
        @weakify(self)
        [[_autoBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            [self btnAction:x];
        }];
    }
    return _autoBtn;
}

- (UIButton *)handBtn {
    if (!_handBtn) {
        _handBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _handBtn.tag = 1000;
        [_handBtn setTitleColor:kRGB(0x1B1B1B) forState:UIControlStateNormal];
        [_handBtn setTitleColor:kBgColor forState:UIControlStateSelected];
        _handBtn.titleLabel.font = kFontRegula(14);
        [_handBtn setTitle:@"跟随商家自动下架，但上架需要手动" forState:UIControlStateNormal];
        @weakify(self)
        [[_handBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            [self btnAction:x];
        }];
    }
    return _handBtn;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:kRGB(0x3898E1) forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = kFontSemiBold(16);
        @weakify(self)
        [[_cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            [self hide];
        }];
    }
    return _cancelBtn;
}

- (void)show {
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = kRGB_ALPHA(0x000000, 0.5);
    self.tag = 7777;
    self.hidden = NO;
    self.alpha = 0;
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
    }];
}
- (void)hide {
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [touches.anyObject locationInView:self];
    if (!CGRectContainsPoint(self.contentView.frame, point)) {
        [self hide];
    }
}

@end
